from src.base.BaseViewControl import *

"""游戏结束界面，所有界面都继承BaseViewControl"""


class GameOverVC(BaseViewControl):

    def __init__(self):
        super(GameOverVC, self).__init__()
        #  初始化界面按钮提示文字等
        self.again_image = pygame.image.load(BASE_RESOURCE_PATH_IMAGE+"again.png").convert_alpha()
        self.gameover_image = pygame.image.load(BASE_RESOURCE_PATH_IMAGE+"gameover.png").convert_alpha()
        self.gameover_rect = self.gameover_image.get_rect()
        self.again_rect = self.again_image.get_rect()
        self.gameover_rect.left, self.gameover_rect.top = \
            ((SCREEN_RECT.width - self.gameover_rect.width) // 2, SCREEN_RECT.height // 2)
        self.again_rect.left, self.again_rect.top = \
            ((SCREEN_RECT.width - self.again_rect.width) // 2, SCREEN_RECT.height // 2 - self.gameover_rect.height - 10)

        self.font = pygame.font.SysFont("", 40)
        self.over_text = self.font.render('GAME OVER!', True, color_green)

    def show(self):
        self.runTime()

    def _onDraw(self):
        super(GameOverVC, self)._onDraw()
        self.screen.blit(self.again_image, self.again_rect)
        self.screen.blit(self.gameover_image, self.gameover_rect)
        self.screen.blit(self.over_text, (self.again_rect.left + 50, self.again_rect.top - 80))

    def _handEvent(self):
        # 获取事件
        for event in pygame.event.get():
            # 判断事件是否为退出事件
            if event.type == pygame.QUIT:
                # 退出pygame
                self.exitApp()
            elif event.type == pygame.MOUSEBUTTONDOWN:
                pos = pygame.mouse.get_pos()
                if self.again_rect.left < pos[0] < self.again_rect.right and \
                        self.again_rect.top < pos[1] < self.again_rect.bottom:
                    self.running = False
                    pygame.event.post(pygame.event.Event(pygame.USEREVENT, {"action": "play"}))
                elif self.gameover_rect.left < pos[0] < self.gameover_rect.right and \
                        self.gameover_rect.top < pos[1] < self.gameover_rect.bottom:
                    self.running = False
                    pygame.event.post(pygame.event.Event(pygame.USEREVENT, {"action": "exit"}))
